<?php
/**
 * Created by PhpStorm.
 * User: Jmashimaro
 * Date: 2015/1/11
 * Time: 17:16
 */
/**
 * 内置用户中心连接接口
 * @author Jmashimaro
 *
 */
class SmslogsPassport extends BasePassport
{
    public $_name = 'smslogs';
}

/**
 *  内置用户中心的用户操作
 *  @usage   none
 */
class SmslogsPassportUser extends BasePassportUser {

    /**
     * 用户注册API发送验证码
     * @param $phone_mob
     * @param $verify_code
     * @return number
     * @author jiayanxiu
     * @date 2015/01/09
     */
    public function verify($phone_mob, $verify_code) {
        $time = time();
        $local_data['phone_mob']   = $phone_mob;
        $local_data['verify_code'] = $verify_code;
        $local_data['created_at']  = $time;

        /**添加到短信日志系统**/
        $id = $this->_local_add($local_data, 'smslogs');

        return $id;
    }


    /**
     * @param string $phone_mob
     * @param string $verify_code
     * @return int|number
     */
    public function register($phone_mob, $verify_code) {
        $local_data['phone_mob']    = $phone_mob;
        $local_data['verify_code']  = $verify_code;

        /* 添加到用户系统 */
        $user_id = $this->_local_add($local_data);

        return $user_id;
    }

    /**
     * 手机API用户注册数据
     * @see BasePassportUser::edit()
     * @author jiayanxiu
     * @date 2015/01/09
     */
    public function edit($user_id, $user_name, $password) {

        $edit_data['user_name'] = $user_name;
        $edit_data['password']  = md5($password);
        $edit_data['reg_time']  = gmtime();
        //编辑本地数据
        $this->_local_edit($user_id, $edit_data);

        return true;
    }
}


/**
 * 内置用户中心的短信操作
 * @author Garbin
 * @usage none
 */
class SmslogsPassportPM extends BasePassportPM {
    /**
     * 账户名
     * @var string
     */
    private $account = 'cf_shiy';
    /**
     * 密码
     * @var string
     */
    private $password = 'JhhcQL_jnsBN9Al';
    /**
     * 短信提交地址
     * @var string
     */
    private $url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

    /**
     * 发送手机短信验证码
     * @param int $phone_mob
     * @param int $verify_code
     * @return mixed
     * @author jyx
     * @date 2015/10/23
     */
    public function send ($phone_mob, $verify_code) {
        $post_data = "account={$this->account}&password={$this->password}&mobile={$phone_mob}&content=".rawurlencode("您的验证码是：".$verify_code."。请不要把验证码泄露给其他人。");

        $json_str = $this->send_post($this->url, $post_data);

        $rs = $this->xml_to_array($json_str);

        return $rs['SubmitResult'];
    }

    /**
     * 用rpc发送短信
     * @param $mobile
     * @param $content
     * 短信模版如下：
     * "authcode"=>"您的验证码是：{code}。请不要把验证码泄露给其他人。"
     * "after_payment_to_store"=>"尊敬的商家，您在He She贺喜时尚的店铺有订单产生，请登录商家后台，订单管理里面查看详情，请在24小时内安排发货。顺祝商祺。"
     * "yoox_order_notice"=>"您好，YOOX的店铺有订单产生了，订单号为：{order}，请您及时处理！"
     * "delivery_notice"=>"尊贵的陛下，您在He She贺喜钦点的时尚恩宠已快马加鞭奔赴朝中，请您耐心等待。记得常翻He She贺喜牌子哟。"
     */
    public function rpcsms($mobile,$content){
        $client = new Yar_Client(SMS_SERVER);
        //$mobile=13612345678;
        //$content=array("template"=>"yoox_order_notice","order"=>123456);
        $result = $client->inlandSend($mobile,$content);
        return $result;
    }

    /**
     * 发送短信内容
     * @param $phone_mob
     * @param $content
     * @return mixed
     * @author jyx
     * @date 2015/10/23
     */
    public function sendContents($phone_mob, $content) {
        $post_data = "account={$this->account}&password={$this->password}&mobile={$phone_mob}&content=".rawurlencode($content);

        $json_str = $this->send_post($this->url, $post_data);

        $rs = $this->xml_to_array($json_str);

        return $rs['SubmitResult'];
    }
    private function send_post($url, $post_data){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }
    private function xml_to_array($xml){
        $reg = '/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/';
        if(preg_match_all($reg, $xml, $matches)){
            $count = count($matches[0]);
            for($i = 0; $i < $count; $i++){
                $subxml= $matches[2][$i];
                $key = $matches[1][$i];
                if(preg_match( $reg, $subxml )){
                    $arr[$key] = $this->xml_to_array( $subxml );
                }else{
                    $arr[$key] = $subxml;
                }
            }
        }
        return $arr;
    }
}

/**
 *  内置用户中心的好友操作
 *  @author Garbin
 *  @usage none
 */
class SmslogsPassportFriend extends BasePassportFriend {

}

/**
 * 内置用户中心的事件操作
 * @usage none
 */
class SmslogsPassportFeed extends BasePassportFeed {

}